package okl.tdf.components
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.charts.PieChart;
	import mx.charts.chartClasses.ChartBase;
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;
	import mx.controls.ProgressBar;
	import mx.controls.listClasses.ListBase;
	import mx.events.StateChangeEvent;
	import mx.states.State;
	
	import okl.tdf.components.interfaces.IInteractionDisplay;
	import okl.tdf.components.skins.InteractionDisplaySkin;
	
	import spark.components.Button;
	import spark.components.CheckBox;
	import spark.components.Group;
	import spark.components.Label;
	import spark.components.RadioButtonGroup;
	import spark.components.supportClasses.SkinnableComponent;
	
	/**
	 *  Dispatched when the <code>dataProvider</code> property changes.
	 *
	 *  @eventType dataChange
	 */
	[Event(name="dataChange", type="flash.events.Event")]

	/**
	 * 	Main component for display TDF statistics.
	 * 
	 *	@author "oleksandr.kulinchenko@gmail.com"
	 */
	[SkinState("normal")] //no data set
	[SkinState("dataFeed")] 
	public class InteractionDisplay extends SkinnableComponent implements IInteractionDisplay
	{
		public function InteractionDisplay()
		{
			super();
			setStyle("skinClass", InteractionDisplaySkin);
			
	
			//states mirror for skin
			this.states = this.states.concat(
				new State({name: "normal"}),
				new State({name: "dataFeed"})
			);
			currentState="normal";
			addEventListener(StateChangeEvent.CURRENT_STATE_CHANGE, onCurrentStateChange);
		}
		

		//{region CLASS_INTERFACE (IInteractionDisplay contract)
		private var __dataProviderDirty:Boolean;
		private var _dataProvider:ArrayCollection;
		/**
		 * Data collection for display
		 * 
		 * @see okl.tdf.model.InteractionsPerCountry
		 */		
		public function get dataProvider():ArrayCollection
		{
			return _dataProvider;
		}

		public function set dataProvider(value:ArrayCollection):void
		{
			if(_dataProvider==value)return;
			_dataProvider=value;
			__dataProviderDirty=true;
			currentState= (dataProvider==null) ? "normal" : "dataFeed";
			
			dispatchEvent(new Event("dataChange"));
			invalidateProperties();
		}
		
		private var _firstResultTime:Date, __firstResultTimeDirty:Boolean;
		public function set firstResultTime(value:Date):void
		{
			if(_firstResultTime==value)return;
			_firstResultTime=value;
			__firstResultTimeDirty=true;
			invalidateProperties();
		}
		
		private var _lastResultTime:Date, __lastResultTimeDirty:Boolean;
		public function set lastResultTime(value:Date):void
		{
			if(_lastResultTime==value)return;
			_lastResultTime=value;
			__lastResultTimeDirty=true;
			invalidateProperties();
		}
		//}endregion
		
		//{region COMP_LIFE_CYCLE
		override protected function commitProperties():void{
			super.commitProperties();
			if(__dataProviderDirty){

				dgInteractionPerCountry.dataProvider = this.dataProvider;
				if(chartInteractions)chartInteractions.dataProvider=this.dataProvider;
				__dataProviderDirty=false;
			}
			
			
			if(__firstResultTimeDirty){
				lblFirstResultTime.targetTime=_firstResultTime;
				lblSinceFirstResultDuration.targetTime=_firstResultTime;
				__firstResultTimeDirty=false;
			}
			
			if(__lastResultTimeDirty){
				lblLatestResultTime.targetTime=_lastResultTime;
				__lastResultTimeDirty=false;
			}
		}
		//}endregion
		
		//{region SKIN_PARTS
		//TODO: decide what is really part of component (on customer requirement)
		[SkinPart(required="true")]public var dgInteractionPerCountry:ListBase;
		[SkinPart(required="true")]public var lblFirstResultTime:TimeLabel;
		[SkinPart(required="true")]public var lblLatestResultTime:TimeLabel;
		[SkinPart(required="true")]public var lblSinceFirstResultDuration:DurationLabel;
		
		[SkinPart(required="false")]public var chartInteractions:ChartBase;
		[SkinPart(required="false")]public var activityIndicator:ProgressBar;
		[SkinPart(required="false")]public var rbgStates:RadioButtonGroup;
		
		
		/*
		* Skinning section - adding suggestion popup elements events 
		*/
		override protected function partAdded(partName:String, instance:Object):void{
			super.partAdded(partName, instance);
			if(rbgStates==instance){
				rbgStates.addEventListener(Event.CHANGE, onSettingsChange);
			}else if(lblSinceFirstResultDuration==instance){
				lblSinceFirstResultDuration.labelFunction=durationLabelFunction;
			}
		}
		
		override protected function partRemoved(partName:String, instance:Object):void{
			super.partRemoved(partName, instance);	
			super.partAdded(partName, instance);
			if(rbgStates==instance){
				rbgStates.removeEventListener(Event.CHANGE, onSettingsChange);
			}
		}
		
		private function onCurrentStateChange(event:StateChangeEvent):void {
			invalidateSkinState();
		}
		
		override protected function getCurrentSkinState():String {
			return currentState;
		}
		
		protected function onSettingsChange(event:Event):void{
			currentState = rbgStates.selectedValue.toString();		
		}
		
		private function durationLabelFunction(ms:Number):String{
			return ms + "ms";
		}
		//}region
	}
}